Page({
  data: {
    favoriteList: [],
    loading: false,
    finished: false
  },

  onLoad() {
    this.loadFavorites();
  },

  // 加载收藏列表
  async loadFavorites() {
    if (this.data.loading || this.data.finished) return;
    
    this.setData({ loading: true });
    
    try {
      // TODO: 调用获取收藏列表接口
      // const res = await getFavorites();
      const res = {
        data: []
      };
      
      this.setData({
        favoriteList: [...this.data.favoriteList, ...res.data],
        finished: res.data.length < 10
      });
    } catch (error) {
      wx.showToast({
        title: '加载失败',
        icon: 'none'
      });
    } finally {
      this.setData({ loading: false });
    }
  },

  // 取消收藏
  cancelFavorite(event) {
    const { id } = event.currentTarget.dataset;
    wx.showModal({
      title: '提示',
      content: '确定要取消收藏该商品吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            // TODO: 调用取消收藏接口
            // await cancelFavorite(id);
            wx.showToast({
              title: '已取消收藏',
              icon: 'success'
            });
            // 刷新列表
            this.setData({
              favoriteList: this.data.favoriteList.filter(item => item.id !== id)
            });
          } catch (error) {
            wx.showToast({
              title: '操作失败',
              icon: 'none'
            });
          }
        }
      }
    });
  },

  // 跳转到商品详情
  goToDetail(event) {
    const { id } = event.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/product/detail/index?id=${id}`
    });
  }
}); 